#include "BasicSemaphore.h"

namespace utility
{
	BasicSemaphore::BasicSemaphore(void)
	{
		m_hSemaphore = INVALID_HANDLE_VALUE;
	}

	BasicSemaphore::~BasicSemaphore(void)
	{
		if( m_hSemaphore != INVALID_HANDLE_VALUE )
		{
			CloseHandle( m_hSemaphore );
		}
	}

	bool BasicSemaphore::CreateSemaphore( uint32 nInitialCount, uint32 nMaximumCount, cstr_t lpszName /*= NULL */ )
	{
		m_hSemaphore = ::CreateSemaphore( NULL, nInitialCount, nMaximumCount, lpszName );
		return m_hSemaphore != INVALID_HANDLE_VALUE;
	}

	bool BasicSemaphore::ReleaseSemaphore( uint32 nIncrease )
	{
		return ::ReleaseSemaphore( m_hSemaphore, 1, NULL ) == TRUE;
	}

	UINT32 BasicSemaphore::WaiteSemaphore( uint32 nTimeout )
	{
		DWORD nRet = WaitForSingleObject( m_hSemaphore, nTimeout );
		return nRet;
	}
}
